"""
Case Type   : page_repair_function
Case Name   : 使用ALTER SYSTEM方式修改page_repair_function参数测试
Create At   : 2025/09/16
Owner       : jiangyan
Description :
    1、查询page_repair_function默认值
    2、使用ALTER SYSTEM设置page_repair_function为on
    3、验证参数设置结果
    4、恢复参数默认值
Expect      :
    1、显示默认值为off
    2、ALTER SYSTEM方式设置成功
    3、参数值为on
    4、恢复默认值成功
History     :
"""

import unittest
import os
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread

logger = Logger()
COMMONSH = CommonSH("PrimaryDbUser")

class GucAlterSystemTestCase(unittest.TestCase):
    def setUp(self):
        logger.info("==Opengauss_Function_Guc_Page_Repair_Case0002测试开始==")
        self.constant = Constant()
        # 检查数据库集群状态
        is_started = COMMONSH.get_db_cluster_status()
        self.assertTrue("Degraded" in is_started or "Normal" in is_started)

    def verify_parameter_value(self, expected_value):
        """验证参数值是否正确"""
        sql_cmd = "show page_repair_function;"
        result = COMMONSH.execut_db_sql(sql_cmd)
        logger.info(f"参数查询结果: {result}")
        
        # 检查结果中是否包含期望值（排除参数名本身）
        result_without_param = result.lower().replace('page_repair_function', '')
        
        if f" {expected_value}" in result_without_param or f"|{expected_value}" in result_without_param:
            logger.info(f"参数值验证成功: {expected_value}")
            return True
        else:
            self.fail(f"参数值验证失败，期望: {expected_value}，实际结果: {result}")

    def test_guc_alter_system(self):
        # 步骤1：查询page_repair_function默认值，期望：off
        logger.info("步骤1：查询page_repair_function默认值，期望：off")
        self.verify_parameter_value("off")

        # 步骤2：使用ALTER SYSTEM设置page_repair_function为on
        logger.info("步骤2：使用ALTER SYSTEM设置page_repair_function为on")
        alter_cmd = "alter system set page_repair_function to on;"
        sql_cmd = COMMONSH.execut_db_sql(alter_cmd)
        logger.info(sql_cmd)
        self.assertIn("ALTER SYSTEM SET", sql_cmd)

        # 步骤3：检查参数是否设置成功，期望：on
        logger.info("步骤3：检查参数是否设置成功，期望：on")
        self.verify_parameter_value("on")

        # 步骤4：恢复参数默认值为off
        logger.info("步骤4：恢复参数默认值为off")
        alter_cmd = "alter system set page_repair_function to off;"
        sql_cmd = COMMONSH.execut_db_sql(alter_cmd)
        logger.info(sql_cmd)
        self.assertIn("ALTER SYSTEM SET", sql_cmd)

        # 步骤5：验证参数恢复结果，期望：off
        logger.info("步骤5：验证参数恢复结果，期望：off")
        self.verify_parameter_value("off")

    def tearDown(self):
        logger.info("恢复环境")
        # 确保参数恢复为默认值
        sql_cmd = COMMONSH.execut_db_sql("show page_repair_function;")
        if "off" not in sql_cmd:
            reset_cmd = "alter system set page_repair_function to off;"
            COMMONSH.execut_db_sql(reset_cmd)
        
        # 检查集群状态
        is_started = COMMONSH.get_db_cluster_status()
        self.assertTrue("Degraded" in is_started or "Normal" in is_started)
        logger.info("==Opengauss_Function_Guc_Page_Repair_Case0002测试结束==")